<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 发现隐患接口
 */
class Fxyh extends Common
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $fxyhModel = null;
    protected $fxyhCategoryModel = null;
    protected $usersmrzModel=null;

    public function _initialize()
    {
        parent::_initialize();
        $this->fxyhModel = new \app\admin\model\Fxyh();
        $this->fxyhCategoryModel = new \app\admin\model\FxyhCategory();
        $this->usersmrzModel = new \app\admin\model\UserSmrz();
    }

    /**
     * 发现隐患分类列表
     *
     */
    public function getFxyhCategoryList()
    {
        $fxyhCategory = $this->fxyhCategoryModel
            ->select();
        $this->outputData($fxyhCategory);
    }

    /**
     * 发现隐患列表
     */
    public function getFxyhList()
    {
        $page = $this->request->get('page') - 1;
        $page=$page>0?$page:0;
        $limit = $this->request->get('pageSize');
        $limit=$limit>0?$limit:5;
        $start = $page * $limit;
        $status = $this->request->get('status');
        $catid = $this->request->get('catid');
        $uid = $this->request->get('uid');
        $zhuangtai = $this->request->get('zhuangtai');
        $groupId=$this->request->get('group_id');
        $fangwuxinxi = $this->request->get('fangwuxinxi');
        $where=[];
        //按状态
        if ($status) {
            $where['f.status'] = $status;
        }
        //按分类
        if ($catid) {
            $where['f.catid'] = $catid;
        }
        //按用户
        if ($uid) {
            $where['f.uid'] = $uid;
        }
        //按审核状态
        if (isset($zhuangtai)) {
            $where['zhuangtai'] = $zhuangtai;
        }
        //按用户组
        if(isset($groupId)){
            $where['f.groupid']=['like',"%{$groupId}%"];
        }
        //按房屋
        if ($fangwuxinxi) {
            $where['fangwuxinxi'] =$fangwuxinxi;
        }

        $fxyhList = $this->fxyhModel
            ->alias('f')
            ->join('fxyhCategory fc', 'f.catid=fc.id')
            ->where($where)
            ->field('f.*,fc.name')
            ->limit($start, $limit)
            ->order('zhuangtai desc')
            ->select();
        foreach ($fxyhList as &$value){
            $value['inputtime']=$this->getTime($value['inputtime']);
            $value['zhuangtai']=str_replace([0,1,2,3],['待处理','处理中','已处理','已核销'],$value['zhuangtai']);
            $fwxx=Db('fwxx')->where('id',$value['fangwuxinxi'])->value('title');
            if($value['image']){
                $value['image']=explode(',',$value['image']);
            }
            $value['fwxx']=$fwxx;
            $value['toale']=Db('fxyh')->where('fangwuxinxi',$value['fangwuxinxi'])->count();
        }
        $this->outputData($fxyhList);
    }

    /**
     * 发现隐患
     */
    public function getFxyhDetail()
    {
        $id = $this->request->get('id');
        if (!is_numeric($id)) {
            $this->outputError('参数错误');
        }
        $fxyhList = $this->fxyhModel
            ->alias('f')
            ->join('fxyhCategory fc', 'f.catid=fc.id')
            ->where('f.id', $id)
            ->field('f.*,fc.name')
            ->find();
        $fxyhList['zhuangtai_name']=str_replace([0,1,2,3],['待处理','处理中','已处理','已核销'],$fxyhList['zhuangtai']);
        $fwxx=Db('fwxx')->where('id',$fxyhList['fangwuxinxi'])->value('title');
        $fxyhList['fwxx']=$fwxx;
        $this->outputData($fxyhList);
    }
    /**
     * 提交隐患
     */
    public function getFxyhAdd()
    {
        $uid=$this->request->request('uid');
        $author=$this->request->request('author');
        $title=$this->request->request('title');
        $catid=$this->request->request('catid');
        $fangwuxinxi=$this->request->request('fangwuxinxi');
        $description=$this->request->request('description');
        $inputtime=time();
        $image=$this->request->request('image');
        if(is_array($image)) {
            $image = join(',', $image);
        }
        //判断当前用户是否实名认证
//        $smrz=$this->usersmrzModel->where(['uid'=>$uid,'status'=>1])->value('id');
//        if(!$smrz){
//            $this->outputError('用户未实名认证');
//        }
        if(!$uid||!$author||!$fangwuxinxi||!$description||!$inputtime){
            $this->outputError('请填写完整的信息');
        }

        $fwxx=Db('fwxx')->where('id',$fangwuxinxi)->value('title');
        $data=['uid'=>$uid,'author'=>$author,'title'=>$fwxx,'catid'=>$catid,'fangwuxinxi'=>$fangwuxinxi,'description'=>$description,'inputtime'=>time(),'image'=>$image];
        $re = $this->fxyhModel
            ->insert($data);
        if($re){
            $this->outputData('投诉成功');
        }else{
            $this->outputData('投诉失败');
        }
    }
    /**
     * 核销隐患
     */
    public function getFxyhUpdate()
    {
        $uid=$this->request->request('uid');
        $id=$this->request->request('id');
        $zhuangtai=$this->request->request('zhuangtai');
        $groupid='';
        switch ($zhuangtai){
            case 1:
                $groupid='2';
                break;
            case 2:
                $groupid='3';
                break;
            case 3:
                $groupid='2,3';
                break;
        }
        if(!$id||!$zhuangtai){
            $this->outputError('请填写完整的信息');
        }

        $re = $this->fxyhModel
            ->where('id',$id)
            ->update(['operator'=>$uid,'groupid'=>$groupid,'zhuangtai'=>$zhuangtai,'updatetime'=>time()]);
        if($re){
            $this->outputData('核销成功');
        }else{
            $this->outputData('核销失败');
        }
    }
}
